import { request } from "../../request/index.js"


// pages/search/search.js
Page({

  data: {
    goods: [],
    // 按钮是否显示
    isFocus: false,
    //input内容
    inputText:""
  },
  TimeId: -1,
  // 输入框
  handleInput(e) {
    //获取输入框的值
    const { value } = e.detail
    //验证合法
    if (!value.trim()) {
      //不合法
      setTimeout(() => {
        this.setData({
          goods: [],
          isFocus: false
        })
      }, 1000);
      return
    }
    //合法
    this.setData({
      isFocus: true
    })
    clearTimeout(this.TimeId)
    this.TimeId = setTimeout(() => {
      this.qSearch(value)
    }, 1000);
  },
  //发送请求
  async qSearch(query) {
    const res = await request({ url: "/goods/qsearch", data: { query } })
    console.log(res);
    this.setData({
      goods: res.data.message
    })
  },
  //点击清除
  handleCancel(){
    this.setData({
      goods:[],
      isFocus:false,
      inputText:""
    })
  }


})